import { createRouter, createWebHistory } from "vue-router";
import Add01 from "../views/children/Add01.vue";
import StaffTable from "../views/children/StaffTable.vue";

const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    // 没有匹配到path时转到404页面
    {
      path: "/404",
      name: "NoPage 404",
      component: () => import("../views/404.vue"),
      hidden: true,
    },
    {
      path: "/:pathMatch(.*)",
      redirect: "/404",
      hidden: true,
    },
    // 根目录
    {
      path: "/",
      name: "index",
      component: () => import("../views/HomeView.vue"),
      redirect: "/home",
      children: [
        {
          path: "home",
          name: "主页",
          component: () => import("../views/children/Home.vue"),
        },
        {
          path: "staff_table",
          name: "最低安全配员标准表",
          component: StaffTable,
        },
        {
          path: "datav",
          name: "表格数据",
          component: () => import("../views/children/Datav.vue"),
        },
        {
          path: "add01",
          name: "新增配员",
          component: Add01,
        },
        {
          path: "standard_vis",
          name: "配员标准可视化",
          component: () => import("../views/children/StandardVis.vue"),
        },
        {
          path: "data_checkrecord",
          name: "船舶核查统计",
          component: () => import("../views/children/ShipCheckRecord.vue"),
        },
        {
          path: "data_crewjob",
          name: "船员任解职查询",
          component: () => import("../views/children/CrewJobPage.vue"),
        },
        {
          path: "demo01",
          name: "demo01",
          component: () => import("../views/children/Demo01.vue"),
        },
      ],
    },
    {
      path: "/about",
      name: "about",
      component: () => import("../views/AboutView.vue"),
    },
    {
      path: "/login",
      name: "登录页",
      component: () => import("../views/LoginView.vue"),
    },
    {
      path: "/test",
      name: "测试页",
      component: () => import("../views/TestView.vue"),
    },
    {
      path: "/vis",
      name: "可视化界面",
      component: () => import("../views/Visualization.vue"),
    },
  ],
});

// 设置登录过期时间（一天）86400000
let expire = 86400000;
//路由守卫    全局守卫，登录拦截，超时登出
//进行路由拦截

// router.beforeEach((to, from, next) => {
//     // 从本地缓存中获取保存的 token 信息
//     const tokenObj = JSON.parse(window.localStorage.getItem("isLogin"));
//     if (to.path === "/login") {
//         next();
//     } else {
//         // 如果没有token，强制跳转到登录页面；如果有，则判断token时间是否过期
//         if (!tokenObj || !tokenObj.token) {
//             next("/login");
//         } else {
//             let date = new Date().getTime();
//             // 当前时间 - token中的登录时间 > 设置的过期时间，为过期；则清除token，并强制跳转至登录页
//             // 反之为有效期，则放行
//             if (date - tokenObj.startTime > expire) {
//                 window.localStorage.removeItem("isLogin");
//                 next("/login");
//                 ElMessage({
//                     message: h("p", null, [
//                         h("span", null, "登录状态过期"),
//                         h("i", {style: "color: teal"}, "请重新登录！"),
//                     ]),
//                 });
//             } else {
//                 next();
//             }
//         }
//     }
// });

export default router;
